import { ElNotification, ElMessageBox } from "element-plus";
/*通知提示 */
export function toast(message, type = "success", parseHtml = false) {
  ElNotification.closeAll();
  ElNotification({
    message,
    type,
    duration: 3000,
    dangerouslyUseHTMLString: parseHtml,
  });
}

export function toastError(message, type = "error", parseHtml = false) {
  ElNotification.closeAll();
  ElNotification({
    message,
    type,
    duration: 3000,
    dangerouslyUseHTMLString: parseHtml,
  });
}

/*确认提示 */
export function showModal(content = "", type = "warning", title = "") {
  return ElMessageBox.confirm(title, content, {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type,
  });
}
export function confirm(
  message = "你确定要离开吗？",
  title = "提示",
  type = "warning"
) {
  return new Promise((resolve, reject) => {
    ElMessageBox.confirm(message, title, {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type,
    })
      .then(() => {
        resolve(true);
      })
      .catch(() => {
        reject(false);
      });
  });
}

/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== "" &&
            typeof value[key] !== "undefined"
          ) {
            let params = propName + "[" + key + "]";
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}
